import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth' // 假设使用Pinia管理登录状态
import IndexView from '../views/IndexView.vue'
import HomeView from '../views/Home/HomeView.vue'
import TenantView from '../views/SysManage/Tenant‌/Tenant‌View.vue'


 const constantRoutes= [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/Login.vue'),
  },
 

   {
    path: '/',
    name: 'Index',
    component: IndexView,
    meta: { requiresAuth: true }, // 标记需要登录的路由
    
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/AboutView.vue'),
    meta: { requiresAuth: true } // 标记需要登录的路由
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes
})

const menuList =  [
  {
      path: '/home',
      name: 'home',
      component:"../views/Home/HomeView.vue",
      meta: { title: '首页' ,parentName:"setting" },
  },
  {
      path: '/setting',
      name: 'setting',
      component:"",
      meta: { title: '设置' , icon:"Setting"},
  },
  {
      path: '/tenant',
      name: 'tenant',
      component:"../views/SysManage/Tenant‌/Tenant‌View.vue",
      meta: { title: '租户管理',parentName:"setting" },
  },
  {
      path: '/menu',
      name: 'menu',
      component:"../views/SysManage/Menu/MenuView.vue",
      meta: { title: '菜单管理',parentName:"setting" },
  },
  {
      path: '/permissions',
      name: 'permissions',
      component:"../views/SysManage/Permissions/PermissionsView.vue",
      meta: { title: '权限管理',parentName:"setting" },
  },

  {
      path: '/echartsDemo',
      name: 'echartsDemo',
      component:"",
      meta: { title: 'echartsDemo' , icon:"Setting"},
  },
    {
      path: '/echartsMdt3',
      name: 'echartsMdt3',
      component:"../views/wow/mdt/Demo.vue",
      meta: { title: 'echart地图3',parentName:"echartsDemo" },
  },

  // {
  //     path: '/mdtDemo',
  //     name: 'mdtDemo',
  //     component:"../views/wow/mdt/Demo.vue",
  //     meta: { title: 'echarts地图',parentName:"echartsDemo" },
  // },
 
]


// 动态添加路由方法
export function addRoutes(menuList: any[]) {
  menuList.forEach(route => router.addRoute("Index",{
    path:route.path,
    name:route.name,
    component:()=>import(/* @vite-ignore */ 
      route.component),
    meta:route.meta
  }))
  router.addRoute({ path: '/:pathMatch(.*)', redirect: '/404' })
}


addRoutes(menuList)

router.beforeEach((to) => {
  const auth = useAuthStore()
  if (to.meta.requiresAuth && !auth.isLoggedIn) {
    return { path: '/login', query: { redirect: to.fullPath } } // 记录跳转来源
  }
})
export default router
